import {
  Controller,
  Get,
  Post,
  Body,
  Inject,
  Query,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { SysService } from './sys.service';
import { LoginUserDto } from './dto/login-user.dto';
import { CreateUserDto } from 'src/user-redis/dto/create-user.dto';
import { UserRedisService } from 'src/user-redis/user-redis.service';
import { ForgotUserDto } from './dto/forgot-user.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { Auth } from 'src/core/auth/decorator/auth.decorator';

@Controller('sys')
export class SysController {
  @Inject(UserRedisService)
  private userService: UserRedisService;

  constructor(private readonly sysService: SysService) {}

  // 用户注册
  @Post('registry')
  @Auth({
    required: false,
  })
  registry(@Body() createUserDto: CreateUserDto) {
    return this.userService.registry(createUserDto);
  }

  // 用户登录
  @Post('login')
  @Auth({
    required: false,
  })
  login(@Body() loginUserDto: LoginUserDto) {
    return this.userService.login(loginUserDto);
  }

  // 找回密码
  @Post('forgot')
  @Auth({
    required: false,
  })
  forgot(@Body() forgotUserDto: ForgotUserDto) {
    return this.userService.updatePassword(forgotUserDto);
  }
  // 发送找回密码邮箱验证码
  @Get('sendEmailForGorgot')
  @Auth({
    required: false,
  })
  sendEmailForGorgot(@Query() dto: { email: string }) {
    return this.sysService.sendEmailForGorgot(dto.email);
  }

  // 发送注册邮箱验证码
  @Get('sendEmailForRegistry')
  @Auth({
    required: false,
  })
  sendEmailForRegistry(@Query() dto: { email: string }) {
    return this.sysService.sendMailForRegistry(dto.email, '注册验证码');
  }

  // 上传文件
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(
    @UploadedFile() file: Express.Multer.File,
    @Body() data: { type: string },
  ) {
    return this.sysService.upload(file, data.type);
  }
}
